/**
 * 
 */
package se.ricardo.gamesorgs.common.graphics;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;

/**
 * Creates the sidepanel of the GUI for selecting
 * the company and the system.
 * <br>
 * 
 * @author Ricardo Ocaña Leal
 *
 */
public class OptionsPanel extends JPanel {
	private JList<String> companyList;
	private JList<String> systemsList;
	private JList<Integer> amountList;
	
	// Models to add data to the JLists
	private DefaultListModel<String> companyModel =
			new DefaultListModel<>();
	private DefaultListModel<String> systemModel =
			new DefaultListModel<>();
	private DefaultListModel<Integer> amountModel =
			new DefaultListModel<>();
			
	/**
	 * Creates the JPanel with a {@link BorderLayout} as
	 * LayoutManager, and adds the necessary panels.
	 * */
	public OptionsPanel() {
		super(new BorderLayout());
		setPreferredSize(new Dimension(200, 768));
		createOptionsPanel();
	}
	
	/**
	 * Creates part of the options side panel, composed of
	 * {@link JList}s.
	 * <br>
	 * 
	 * @return
	 * The {@link JComponent} to be included in the sidepanel.
	 * */
	private void createOptionsPanel() {
		final JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		companyList = new JList<>(companyModel);
		companyList.setBorder(BorderFactory.createLoweredBevelBorder());
		companyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane compScroll = new JScrollPane(companyList);
		compScroll.setBorder(BorderFactory.createTitledBorder("Companies"));
		
		final JPanel systemsPanel = new JPanel(new BorderLayout());
		systemsList = new JList<>(systemModel);
		systemsList.setBorder(BorderFactory.createLoweredBevelBorder());
		systemsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		amountList = new JList<>(amountModel);
		amountList.setBorder(BorderFactory.createLoweredBevelBorder());
		amountList.setEnabled(false);
		
		systemsPanel.add(systemsList, BorderLayout.CENTER);
		systemsPanel.add(amountList, BorderLayout.EAST);
		
		JScrollPane sysScroll = new JScrollPane(systemsPanel);
		sysScroll.setPreferredSize(new Dimension(50, 100));
		sysScroll.setBorder(BorderFactory.createTitledBorder("Systems"));
				
		p.add(compScroll);
		p.add(sysScroll);
		
		add(p, BorderLayout.CENTER);
	}
	
	/**
	 * Adds listeners to the {@link JList}s, for registering changes
	 * in selection.
	 * <br>
	 * 
	 * @param
	 * listener The {@link ListSelectionListener} to be added.
	 * */
	public void registerItemListener(ListSelectionListener listener) {
		companyList.addListSelectionListener(listener);
		systemsList.addListSelectionListener(listener);
	}
	
	public DefaultListModel<String> getCompanyListModel() {
		return companyModel;
	}
	
	public DefaultListModel<String> getSystemsListModel() {
		return systemModel;
	}
	
	public DefaultListModel<Integer> getAmountListModel() {
		return amountModel;
	}
	
	/**
	 * Returns the name of the currently selected company.
	 * <br>
	 * 
	 * @return
	 * A String with the name of the company.
	 * */
	public String getCompany() {
		return companyList.getSelectedValue();
	}
	
	/**
	 * Returns the name of the currently selected system.
	 * <br>
	 * 
	 * @return
	 * A String with the name of the system.
	 * */
	public String getSystem() {
		return systemsList.getSelectedValue();
	}
	
	/**
	 * Returns the index of the currently selected system.
	 * <br>
	 * 
	 * @return
	 * int representing the index.
	 * */
	public int getSystemsIndex() {
		return systemsList.getSelectedIndex();
	}
	
	public void setCompanyIndex(int index) {
		companyList.setSelectedIndex(index);
	}
	
	public int getCompanyIndex() {
		return companyList.getSelectedIndex();
	}
	
	public void setSystemsIndex(int index) {
		systemsList.setSelectedIndex(index);
	}
}
